<!--
 * @Descripttion: 群发短信
 * @Author: xiao li
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: xiao li
 * @LastEditTime: 2021-04-29 13:48:24
-->
<template>
    <div class="lb-group-news">
        <top-nav></top-nav>
        <div class="page-main">
            <el-tabs type="border-card">
                <el-tab-pane label="短信群发">
                    <el-form @submit.native.prevent :model="newsForm" :rules='newsFormRules' ref='newsForm' label-width="140px">
                        <el-form-item label="发送对象" prop="default_voice_switch">
                            <el-radio-group v-model="newsForm.default_voice_switch">
                                <el-radio :label="0">所有人</el-radio>
                                <el-radio :label="1">员工</el-radio>
                                <el-radio :label="2">非员工</el-radio>
                            </el-radio-group>
                            <lb-tool-tips>只有同意授权了手机号并且保存成功了的才能收到短信</lb-tool-tips>
                        </el-form-item>
                        <el-form-item label="模板CODE" prop="modular">
                            <el-input v-model="newsForm.modular" placeholder="请输入内容"></el-input>
                            <lb-tool-tips>短信模板模板CODE, 如: SMS_129755997</lb-tool-tips>
                        </el-form-item>
                        <el-form-item label="签名" prop="sign">
                            <el-input v-model="newsForm.sign" placeholder="请输入内容"></el-input>
                            <lb-tool-tips>签名在阿里云短信签名管理处查看</lb-tool-tips>
                        </el-form-item>
                        <el-form-item>
                            <lb-button type='primary' @click="submitFormInfo('newsForm')">{{$t('action.submit')}}</lb-button>
                        </el-form-item>
                    </el-form>
                </el-tab-pane>
                <el-tab-pane label="群发配置">
                    <el-form @submit.native.prevent :model="configForm" :rules='configFormRules' ref='configForm' label-width="140px">
                        <el-form-item label="阿里云ID" prop="aliyun_sms_access_key_id">
                            <el-input v-model="configForm.aliyun_sms_access_key_id" placeholder="请输入内容"></el-input>
                            <lb-tool-tips>此处应填写自己的阿里云AccessKey ID, 登录自己的阿里云账号, 鼠标放到自己的头像处, 会展示出一个列表, 点击列表中的accesskeys就能查看到信息</lb-tool-tips>
                        </el-form-item>
                        <el-form-item label="阿里云密匙" prop="aliyun_sms_access_key_secret">
                            <el-input v-model="configForm.aliyun_sms_access_key_secret" placeholder="请输入内容"></el-input>
                            <lb-tool-tips>此处应填写自己的阿里云Access Key Secret, 登录自己的阿里云账号, 鼠标放到自己的头像处, 会展示出一个列表, 点击列表中的accesskeys就能查看到信息</lb-tool-tips>
                        </el-form-item>
                        <el-form-item>
                            <lb-button type='primary' @click="submitFormInfo('configForm')">{{$t('action.submit')}}</lb-button>
                        </el-form-item>
                    </el-form>
                </el-tab-pane>
            </el-tabs>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      newsForm: {
        default_voice_switch: 0,
        modular: '',
        sign: ''
      },
      newsFormRules: {
        default_voice_switch: {required: true, type: 'number', message: '请选择类型', trigger: 'change'},
        modular: {required: true, type: 'string', message: '请输入内容', trigger: 'blur'},
        sign: {required: true, type: 'string', message: '请输入内容', trigger: 'blur'}
      },
      configForm: {
        aliyun_sms_access_key_id: '',
        aliyun_sms_access_key_secret: ''
      },
      configFormRules: {
        aliyun_sms_access_key_id: {required: true, type: 'string', message: '请输入内容', trigger: 'blur'},
        aliyun_sms_access_key_secret: {required: true, type: 'string', message: '请输入内容', trigger: 'blur'}
      }
    }
  },
  created () {
    this.getAliyunFormInfo()
  },
  methods: {
    submitFormInfo (name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          let subForm = this[name]
          if (name === 'newsForm') {
            this.$api.appSettingShortMsg(subForm).then(res => {
              if (res.code === 200) {
                this.tipsSuccess()
              }
            })
          } else {
            this.$api.appSettingMsgConfig(subForm).then(res => {
              if (res.code === 200) {
                this.tipsSuccess()
              }
            })
          }
        }
      })
    },
    getAliyunFormInfo () {
      this.$api.getAliyunInfo().then(res => {
        if (res.code === 200) {
          for (let key in this.configForm) {
            this.configForm[key] = res.data[key]
          }
        }
      })
    },
    tipsSuccess () {
      this.$message.success(this.$t('tips.successSub'))
    }
  }
}
</script>

<style lang="scss" scoped>
    .lb-group-news{
        width: 100%;
        .page-main{
            .el-input{
                width: 300px;
            }
        }
    }
</style>
